#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

void crearTabla(sql::Statement *stmt)
{
  string name;
  cout << "Nombe de la tabla: ";
  cin >> name;
  cout << endl;
  stmt->execute("CREATE TABLE " + name + " (id INT,data VARCHAR(100))");
}

void agregarDato(sql::Statement *stmt)
{
  sql::ResultSet *res;
  string TableName, querry, dato;
  int i = 1;
  cout << "Nombe de la tabla en la que agregar: ";
  cin >> TableName;
  cout << endl;
  res = stmt->executeQuery("SHOW columns FROM " + TableName);
  
  querry = "INSERT INTO " + TableName + " VALUES ('";
  while(res->next()) 
  {
    cout << "Escribir dato para " << res->getString(1) << " :" << endl;
    cin >> dato;
    cout << endl;
    querry = querry + dato + "','";
  }
  querry.erase(querry.size()-2);
  querry = querry + ")";
  stmt->execute(querry);
  cout << "Agregado!" << endl;
}

void mostrarDatos(sql::Statement *stmt)
{
  string TableName;
  sql::ResultSet *datos, *metadatos;
  cout << "Nombe de la tabla a mostrar: ";
  cin >> TableName;
  cout << endl;
  datos = stmt->executeQuery("SELECT * FROM " + TableName);
  metadatos = stmt->executeQuery("SHOW columns FROM " + TableName);
  cout << "Tabla:" << endl;
  while(metadatos->next())
  {
    cout << metadatos->getString(1) << "\t";
  }
  cout << endl;
  while(datos->next()) 
  {
    
    cout << datos->getString(1) << "\t" << datos->getString(2) << endl;
  }
  cout << "Fin tabla" << endl;
}


void eliminarTabla(sql::Statement *stmt)
{
  string TableName;
  cout << "Nombe de la tabla a eliminar: ";
  cin >> TableName;
  stmt->execute("DROP TABLE IF EXISTS " + TableName);
  cout << "Tabla eliminada!";
}

int main(void)
{
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;
  bool end = false;
  int opcion = 0;
  cout << "Iniciando...";
  
  
  try{
    /* Create a connection */
    driver = get_driver_instance();
    con = driver->connect("tcp://127.0.0.1:3306", "root", "popote");
    /* Connect to the MySQL test database */
    con->setSchema("test");
    stmt = con->createStatement();
    
  }catch (sql::SQLException &e){
    cout << "Imposible Iniciar!!!" << endl;
    cout << "# ERR: SQLException in " << __FILE__;
    cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
    cout << "# ERR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << " )" << endl;
  }
  
  while(!end)
  {
  try {
    cout << endl << endl << endl << endl;
    cout << "******* Menu**********" << endl;
    cout << "1)Crear Tabla" << endl;
    cout << "2)Agregar datos a tabla" << endl;
    cout << "3)Mostrar datos de la tabla" << endl;
    cout << "4)Eliminar tabla" << endl;
    cout << "5)Terminar programa" << endl;
    cin >> opcion;
    if(opcion == 1)
    {
      crearTabla(stmt);
    }
    if(opcion == 2)
    {
      agregarDato(stmt);
    }
    if(opcion == 3)
    {
      mostrarDatos(stmt);
    }
    if(opcion == 4)
    {
      eliminarTabla(stmt);
    }
    if(opcion == 5)
      end = true;

    } catch (sql::SQLException &e) {
    cout << "# ERR: SQLException in " << __FILE__;
    cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
    cout << "# ERR: " << e.what();
    cout << " (MySQL error code: " << e.getErrorCode();
    cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

  }
  cout << "Good day..." << endl;
  delete res;
  delete stmt;
  delete con;


return EXIT_SUCCESS;
}
